package com.lyt.springboot.util.algorithm;

import java.util.Stack;

public class S3 {
    Stack<Character> stack = new Stack<>();

    public boolean inValid(String x) {
        for (char c : x.toCharArray()) {
            if (c == '[') {
                stack.push(']');

            } else if (c == '{') {
                stack.push('}');
            } else if (c == '(') {
                stack.push(')');
            } else {
                if (stack.empty()) {   //如果只有右括号 没有左括号 --》非法
                    return false;

                } else if (stack.pop() != c) {
                    return false;

                }

            }
        }
        if (stack.empty()) {
            return true;

        } else {
            return false;
        }
    }
}
